﻿unit Unit6;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB,
   UniProvider, SQLiteUniProvider,DBAccess, Uni, MemDS, VirtualTable,DataSetDelta,
    Vcl.StdCtrls, Vcl.Grids, Vcl.DBGrids;

type
  TForm6 = class(TForm)
    DBGrid1: TDBGrid;
    Memo1: TMemo;
    Button2: TButton;
    DBGrid2: TDBGrid;
    Button3: TButton;
    Button1: TButton;
    ComboBox1: TComboBox;
    Button4: TButton;
    DataSource1: TDataSource;
    VirtulTable1: TVirtualTable;
    DataSource2: TDataSource;
    UniConnection1: TUniConnection;
    UniQuery1: TUniQuery;
    SQLiteUniProvider1: TSQLiteUniProvider;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    procedure Button4Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  private
    { Private declarations }
  public
    dcm1:TQFDataSetMonitor;
    dcm2:TQFDataSetMonitor;
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}


procedure TForm6.FormCreate(Sender: TObject);
begin
  dcm1:=TQFDataSetMonitor.Create(nil);
  dcm1.DataSet:=UniQuery1;
  dcm2:=TQFDataSetMonitor.Create(nil);
  dcm2.DataSet:=VirtulTable1;
end;

procedure TForm6.FormDestroy(Sender: TObject);
begin
  if Assigned(dcm1) then freeandnil(dcm1);
  if Assigned(dcm2) then freeandnil(dcm2);
end;

procedure TForm6.Button1Click(Sender: TObject);
begin
  if dcm1.Active then
  begin
    dcm1.Active:=false;
    dcm2.Active:=false;
    Button1.caption:='开启监控';
  end
  else
  begin
    dcm1.Active:=true;
    dcm2.Active:=true;
    Button1.caption:='停止监控';
  end;
end;

procedure TForm6.Button2Click(Sender: TObject);
var
  sql:string;
begin
  if UniQuery1.State in [dsEdit, dsInsert] then
    UniQuery1.Post;
  sql:=dcm1.GetActionSQL('table1');
  if sql<>'' then
    memo1.Lines.Add(sql);
end;

procedure TForm6.Button3Click(Sender: TObject);
var sql:string;
begin
  if VirtulTable1.State in [dsEdit, dsInsert] then
    VirtulTable1.Post;
  sql:=dcm2.GetActionSQL('table2');
  if sql<>'' then
    memo1.Lines.Add(sql);
end;

procedure TForm6.Button4Click(Sender: TObject);
begin
  UniConnection1.Disconnect;
  {$IFDEF FPC}
    {$ifdef linux}
    SQLiteLibraryName:=ExtractFilePath(Application.ExeName)+'libsqlite3.so';
    {$else}
    SQLiteLibraryName:=utf8tocp936(ExtractFilePath(Application.ExeName)+'sqlite3.dll');
    {$endif}
    UniConnection1.Protocol:='sqlite';
    UniConnection1.LibraryLocation:=utf8tocp936(ExtractFilePath(Application.ExeName)+{$ifdef linux}'libsqlite3.so'{$else}'sqlite3.dll'{$endif});
    UniConnection1.Properties.Clear;
    UniConnection1.Properties.Add('encrypted=yes');
    UniConnection1.Properties.Add('cipher=sqlcipher');
    UniConnection1.Properties.Add('sqlcipher=legacy');
    UniConnection1.Properties.Add('legacy=1');
    UniConnection1.Properties.Add('controls_cp=CP_UTF8');
    UniConnection1.Properties.Add('AutoEncodeStrings=True');
   {$ELSE}

   {$ENDIF}


  UniConnection1.Database:='config.db';
//  UniConnection1.Password:='123asd';
  UniConnection1.Connect;
  UniConnection1.GetTableNames(combobox1.Items);
  combobox1.ItemIndex:=0;
  //QFDataSetMonitor1.Active:=true;

end;

procedure TForm6.Button5Click(Sender: TObject);
begin
  UniQuery1.Close;
  UniQuery1.SQL.Text:='select * from '+combobox1.Text;
  UniQuery1.Open;
  if dcm1.Active then
  begin
    Button1.caption:='停止监控';
  end
  else
  begin
    Button1.caption:='开启监控';
  end;
end;

procedure TForm6.Button6Click(Sender: TObject);
var
  lstream:TMemoryStream;
begin
  lstream:=TMemoryStream.Create;
  UniQuery1.SaveToXML(lstream);
  lstream.Position:=0;
  VirtulTable1.LoadFromStream(lstream);
  VirtulTable1.Open;
end;

procedure TForm6.Button7Click(Sender: TObject);
begin
   UniQuery1.Append;
   UniQuery1.FieldByName('id').AsString:='1';
   UniQuery1.FieldByName('name').AsString:='001';
   UniQuery1.FieldByName('sq').AsString:='002';
   UniQuery1.Post;


   UniQuery1.Append;
   UniQuery1.FieldByName('id').AsString:='2';
   UniQuery1.FieldByName('name').AsString:='002';
   UniQuery1.FieldByName('sq').AsString:='003';
   UniQuery1.Post;
end;


procedure TForm6.Button8Click(Sender: TObject);
begin
   VirtulTable1.edit;
   VirtulTable1.FieldByName('name').AsString:='test';
   VirtulTable1.FieldByName('sq').AsString:='test02';
   VirtulTable1.Post;

end;

end.
